package com.jvxb.manage.livable.controller;

import com.jvxb.manage.livable.entity.DbMonitorData;
import com.jvxb.manage.livable.service.DbMonitorDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.jvxb.common.web.RespMsg;

/**
 * @author jvxb
 * @since 2022-02-15
 */
@RestController
@RequestMapping("/livable/dbMonitorData/v1")
public class DbMonitorDataController {

    @Autowired
    private DbMonitorDataService dbMonitorDataService;

    @PostMapping("/add")
    public RespMsg add(@RequestBody DbMonitorData dbMonitorData) {
        dbMonitorDataService.save(dbMonitorData);
        return RespMsg.ok();
    }

    @PostMapping("/delete")
    public RespMsg delete(@RequestBody DbMonitorData dbMonitorData) {
        dbMonitorDataService.removeById(dbMonitorData);
        return RespMsg.ok();
    }

    @PostMapping("/update")
    public RespMsg update(@RequestBody DbMonitorData dbMonitorData) {
        dbMonitorDataService.saveOrUpdate(dbMonitorData);
        return RespMsg.ok();
    }

    @GetMapping("/getById")
    public RespMsg getById(Long id) {
        DbMonitorData dbMonitorData = dbMonitorDataService.getById(id);
        return RespMsg.ok(dbMonitorData);
    }

    @GetMapping("/list")
    public RespMsg list() {
        List<DbMonitorData> list = dbMonitorDataService.list(null);
        return RespMsg.ok(list);
    }

    @GetMapping("listPage")
    public RespMsg listPage(@RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<DbMonitorData> dbMonitorDataIPage = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        dbMonitorDataService.page(dbMonitorDataIPage, wrapper);
        return RespMsg.ok(dbMonitorDataIPage);
    }
}
